package cn.stylefeng.gen.core;

import cn.stylefeng.gen.modular.model.TableFieldData;
import com.alibaba.fastjson.JSONObject;

import java.util.List;

/**
 * 加工api json工具类
 *
 * @author yuelinbo
 * @date 2019/2/20 17:16
 **/
public class JsonApiUtil {

    public static JSONObject packJson(List<TableFieldData> fieldInfos) {

        JSONObject json = new JSONObject();
        json.put("$schema", "http://json-schema.org/draft-04/schema#");
        json.put("type", "object");
        //key properties 用于存放数据
        JSONObject parentPropertiesJson = new JSONObject();


        JSONObject json1 = new JSONObject();
        JSONObject json2 = new JSONObject();
        JSONObject json3 = new JSONObject();
        JSONObject json4 = new JSONObject();
        JSONObject json5 = new JSONObject();
        JSONObject json6 = new JSONObject();

        json1.put("type", "number");
        json2.put("type", "string");
        parentPropertiesJson.put("code", json1);
        parentPropertiesJson.put("msg", json2);


        JSONObject childPropertiesJson = new JSONObject();
        JSONObject dataJson = new JSONObject();
        JSONObject recordsJson = new JSONObject();
        dataJson.put("type", "object");

        json3.put("type", "number");
        json4.put("type", "number");
        json5.put("type", "number");
        json6.put("type", "number");

        childPropertiesJson.put("current", json3);
        childPropertiesJson.put("pages", json4);
        childPropertiesJson.put("size", json5);
        childPropertiesJson.put("total", json6);


        recordsJson = packRecords(recordsJson, fieldInfos);


        childPropertiesJson.put("records", recordsJson);
        dataJson.put("properties", childPropertiesJson);
        parentPropertiesJson.put("data", dataJson);

        json.put("properties", parentPropertiesJson);

        return json;
    }

    //records 数据组装
    private static JSONObject packRecords(JSONObject recordsJson, List<TableFieldData> fieldInfos) {

        recordsJson.put("type", "array");
        JSONObject itemsJson = new JSONObject();
        JSONObject propertiesInnerJson = new JSONObject();

        itemsJson.put("type", "object");
        for (TableFieldData fieldInfo : fieldInfos) {
            JSONObject jsonObjectInner = new JSONObject();
            jsonObjectInner.put("type", dispatcherType(fieldInfo.getType()));
            jsonObjectInner.put("description", fieldInfo.getComment());
            propertiesInnerJson.put(fieldInfo.getPropertyName(), jsonObjectInner);
        }


        itemsJson.put("properties", propertiesInnerJson);


        recordsJson.put("items", itemsJson);


        return recordsJson;

    }

    //数据类型转换
    private static String dispatcherType(String type) {

        if (type.contains("varchar")) {
            return "string";
        } else if (type.contains("float") || type.contains("integer") || type.contains("double") || type.contains("tinyint") || type.contains("bigint") || type.contains("decimal")) {
            return "number";
        }

        return "string";


    }
}
